<template>
  <div class="show-main">
    <el-scrollbar style="height:100%">
      <el-form :inline="true" :model="searchForm" ref="searchForm" class="demo-form-inline" size="small">
        <el-form-item label="">
          <el-input v-model="searchForm.name" placeholder="请输入名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="searchForm.type" clearable placeholder="选择套餐类型">
            <el-option label="荤菜" :value="1"></el-option>
            <el-option label="素菜" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="searchForm.is_show" clearable placeholder="请选择上架状态">
            <el-option label="已上架" :value="1"></el-option>
            <el-option label="已下架" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.stop="search(true)">搜索</el-button>
          <el-button type="success" @click.stop="search">刷新</el-button>
          <el-button type="warning" @click.stop="add">添加</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="multiOff" style="margin-bottom:10px">批量下架</el-button>
      <el-table :data="table" border size="small" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="stock" label="库存">
          <template slot-scope="scope">
            <el-button type="text" @click.stop="editStock(scope.row)">{{scope.row.stock}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1" style="color:#409EFF">荤菜</span>
            <span v-else-if="scope.row.type === 2" style="color:#F56C6C">素菜</span>
            <span v-else style="color:#909399">未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="detail" label="上架">
          <template slot-scope="scope">
            <span>
              <el-tooltip :content="scope.row.is_show == 1 ? '上架': '下架'" placement="top" :enterable="false">
                <el-switch v-model="scope.row.is_show" :active-value="1" :inactive-value="0" @change="changeShow(scope.row)">
                </el-switch>
              </el-tooltip>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="150"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchForm.page"
          :page-sizes="[10,20,30,40]" :page-size="searchForm.limit" layout="total, sizes, prev, pager, next, jumper" :total="count">
        </el-pagination>
      </div>
      <el-dialog title="添加套餐" :visible.sync="addVisible" width="500px" :before-close="handleClose('addForm')">
        <el-form ref="addForm" :model="addForm" label-width="140px" size="small" :rules="rules">
          <el-form-item label="套餐类型" prop="type">
            <el-select v-model="addForm.type" clearable placeholder="选择套餐类型" style="width:100%">
              <el-option label="荤菜" :value="1"></el-option>
              <el-option label="素菜" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="addForm.name" placeholder="选择名称" clearable></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('addVisible', 'addForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('addVisible','addForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="编辑商品" :visible.sync="editVisible" width="500px" :before-close="handleClose('editForm')">
        <el-form ref="editForm" :model="editForm" label-width="140px" size="small" :rules="rules">
          <el-form-item label="套餐类型" prop="type">
            <el-select v-model="editForm.type" clearable placeholder="选择套餐类型" style="width:100%">
              <el-option label="荤菜" :value="1"></el-option>
              <el-option label="素菜" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="editForm.name" placeholder="选择名称" clearable></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('editVisible', 'editForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('editVisible','editForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import { editGoodsConfStock, goodsConfList, changeGoodsConfShow, getGoodsConfOnly, deleteGoodsConf, addGoodsConf, editGoodsConf, confMultiOff } from '@/network/api.js'

export default {
  name: 'goodsconf',
  data() {
    return {
      searchForm: {
        page: 1,
        limit: 10,
        name: '',
        type: '',
        is_show: ''
      },
      count: 0, // 总记录数
      table: [],
      multipleSelection: [],
      addForm: {
        name: '', // 名称
        type: '' //
      }, // 添加的表单
      editForm: {
        id: 0,
        name: '', // 名称
        type: '' //
      }, // 编辑的表单
      addVisible: false, // 添加表单
      editVisible: false, // 编辑表单
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'change' }]
      }
    }
  },
  created() {
    this.search()
  },
  computed: {},
  methods: {
    async search(pageInit = false) {
      try {
        if (pageInit === true) {
          this.searchForm.page = 1
        }
        const rtn = await goodsConfList(this.searchForm)
        this.table = rtn.data.list
        this.count = rtn.data.count
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.search()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.search()
    },
    // 列表左侧选中
    handleSelectionChange(val) {
      this.multipleSelection = []
      val.forEach(item => {
        this.multipleSelection.push(item.id)
      })
    },
    // 批量下架
    async multiOff() {
      if (this.multipleSelection.length === 0) {
        return this.$message.error('请至少选中一条数据')
      }
      try {
        await confMultiOff({ ids: this.multipleSelection })
        this.search()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加商品
    add() {
      this.addVisible = true
    },
    // 编辑库存
    editStock(row) {
      this.$prompt('请输入库存', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[1-9][0-9]{0,}$/,
        inputPlaceholder: row.stock,
        inputErrorMessage: '请输入合法的数字'
      })
        .then(async ({ value }) => {
          try {
            await editGoodsConfStock({ id: row.id, stock: value })
            row.stock = value
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {
          this.$message.warning('您取消了这次操作')
        })
    },
    // 编辑商品
    async edit(id) {
      try {
        const rtn = await getGoodsConfOnly({ id })
        this.editVisible = true
        this.$nextTick(() => {
          this.editForm = rtn.data
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 删除
    async del(id) {
      try {
        await deleteGoodsConf({ id })
        this.search()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 修改套餐上下架
    async changeShow(row) {
      try {
        await changeGoodsConfShow({ id: row.id, is_show: row.is_show })
      } catch (e) {
        row.is_show = row.is_show === 1 ? 2 : 1
        this.$message.error(e.message)
      }
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose(formName) {
      return done => {
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close(visibleName, formName) {
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send(visibleName, formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          switch (formName) {
            case 'addForm': // 添加表单
              try {
                await addGoodsConf(this.addForm)
                this.close('addVisible', 'addForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'editForm': // 编辑表单
              try {
                await editGoodsConf(this.editForm)
                this.close('editVisible', 'editForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
